auto.waitFor();
const width = device.width;
const height = device.height;

function hasExecMinute(start) {
  const now = Date.now();
  return (now - start) / 1000 / 60;
}

function next() {
  const x1 = width / 2;
  const y1 = height / 2 + height / 4;

  const x2 = width / 2;
  const y2 = height / 2 - height / 4;

  const duration = 500;

  swipe(
    x1 + random(0, 100),
    y1 + random(0, 100),
    x2 + random(0, 100),
    y2 - random(0, 100),
    duration + random(0, 100)
  );
}

function bezier_curves(cp, t) {
  var ax =
    cp[3].x - cp[0].x - 3.0 * (cp[1].x - cp[0].x) - 3.0 * (cp[2].x - cp[1].x);
  var bx = 3.0 * (cp[1].x - cp[0].x) - 6.0 * (cp[2].x - cp[1].x);
  var cx = 3.0 * (cp[2].x - cp[1].x);

  var ay =
    cp[3].y - cp[0].y - 3.0 * (cp[1].y - cp[0].y) - 3.0 * (cp[2].y - cp[1].y);
  var by = 3.0 * (cp[1].y - cp[0].y) - 6.0 * (cp[2].y - cp[1].y);
  var cy = 3.0 * (cp[2].y - cp[1].y);

  var tSquared = t * t;
  var tCubed = tSquared * t;

  var result = { x: 0, y: 0 };
  result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
  result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;

  return result;
}

function sml_move(qx, qy, zx, zy, time) {
  var point = [
    { x: qx, y: qy },
    { x: qx + Math.random() * 200 - 100, y: qy + Math.random() * 100 },
    { x: zx + Math.random() * 200 - 100, y: zy + Math.random() * 100 },
    { x: zx, y: zy },
  ];

  var xxy = [time];
  for (var i = 0; i <= 1; i += 0.05) {
    var newPoint = bezier_curves(point, i);
    xxy.push([parseInt(newPoint.x), parseInt(newPoint.y)]);
  }

  gesture(xxy);
}

function main() {
  const start = Date.now();
  const maxMinute = random(30, 35);
  while (true) {
    next();
    sleep(random(30 * 1000, 60 * 1000));
    const minute = hasExecMinute(start);
    if (minute > maxMinute) {
      break;
    }
  }
}
